import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'cart_num.dart';
import '../../utils/dio_util.dart';
class ProductValue extends StatefulWidget {
  ProductValue({this.product, this.productValue});

  final List<dynamic> productValue;
  final Map<String, dynamic> product;

  @override
  _ProductValueState createState() => _ProductValueState();
}

class _ProductValueState extends State<ProductValue> {
  List<dynamic> newProductValue = [];
  List<dynamic> productValue = [];
  Map<String, dynamic> product = {};
  var cartProvider;
  Map<String, dynamic> _selectProductValue; //被选中的商品属性

  @override
  void initState() {
    super.initState();
    setState(() {
      productValue = widget.productValue;
      product = widget.product;
    });
    _initProductValue();
  }

  //添加到购物车
  addCart() async{
    int productId=product['productId'];
    int isNew= product['isNew'];
    String unique= _selectProductValue['unique'];
    int cartNum= 1;
    String api='/api/mall/cart/add.service';
    Map<String, dynamic> param = {"cartNum": cartNum,
      "isNew": isNew,
      "productId": productId,
      "uniqueId": unique};
    DioUtil dio = DioUtil();
    var res = await dio.post(api, data: param);
    print(res);
    if(res['code']==200){
      Fluttertoast.showToast(msg: '添加购物车成功');
    }
  }

  //初始化初始化商品属性
  _initProductValue() {
    var _newProductvalue = productValue;
    setState(() {
      newProductValue = _newProductvalue;
      _selectProductValue = _newProductvalue.first;
    });
    print(newProductValue);
    print(1234256);
  }

  @override
  Widget build(BuildContext context) {
    return  Stack(
      children: <Widget>[
           Container(
              padding: EdgeInsets.fromLTRB(20.w,80.w,20.w,0.w),
              child:Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      height: 150.w,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            height: 150.w,
                            width: 200.w,
                            padding: EdgeInsets.only(right: 50.w),
                            decoration: BoxDecoration(
                                borderRadius:
                                BorderRadius.all(Radius.circular(10.0)),
                                image: DecorationImage(
                                    image: NetworkImage(
                                        '${_selectProductValue['image']}'),
                                    fit: BoxFit.cover)),
                          ),
                          Spacer(
                            flex: 1,
                          ),
                          Expanded(
                            flex: 10,
                            child: Container(
                              height: 150.w,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    '${product['productName']}',
                                    style: TextStyle(
                                        color: Colors.black, fontSize: 30.sp),
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                  RichText(
                                    text: TextSpan(children: [
                                      TextSpan(
                                          text: '￥',
                                          style: TextStyle(
                                              color: Colors.red,
                                              fontSize: 30.sp)),
                                      TextSpan(
                                          text:
                                          '${_selectProductValue['price']}',
                                          style: TextStyle(
                                              color: Colors.red,
                                              fontSize: 45.sp)),
                                      TextSpan(
                                          text:
                                          '       库存：${_selectProductValue['stock']}',
                                          style: TextStyle(
                                              color: Colors.grey,
                                              fontSize: 30.sp)),
                                    ]),
                                  )
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                    Divider(),
                    Text(
                      '类型',
                      style:
                      TextStyle(color: Colors.grey[500], fontSize: 30.sp),
                    ),
                    Divider(),
                    Container(
                      height: 250.w,
                      child: SingleChildScrollView(
                        physics: BouncingScrollPhysics(),
                        child: Wrap(
                          children: newProductValue.map((value) {
                            return Container(
                              margin: EdgeInsets.all(5),
                              child: InkWell(
                                onTap: () {
                                  setState(() {
                                    _selectProductValue = value;
                                  });
                                },
                                child: Chip(
                                  label: Text("${value["suk"]}",
                                      style: TextStyle(color: Colors.white)),
                                  padding: EdgeInsets.all(5),
                                  backgroundColor:_selectProductValue==value?Colors.red: Colors.black26,
                                ),
                              ),
                            );
                          }).toList(),
                        ),
                      ),
                    ),
                    Divider(),
                    Text(
                      '数量',
                      style:
                      TextStyle(color: Colors.grey[500], fontSize: 30.sp),
                    ),
                    Divider(),
                    Container(
                      margin: EdgeInsets.only(top: 10),
                      height: 80.h,
                      child: Row(
                        children: <Widget>[
                          CartNum(selectProductValue: _selectProductValue)
                        ],
                      ),
                    ),

                  ]) ,
            ),
        Positioned(
          bottom:0,
          left:10.w,
          child:  Container(
            width:730.w,
            child: RaisedButton(
              color: Colors.red,
              child: Text('确定',style: TextStyle(
                  color:Colors.white
              ),),
              onPressed: (){
                addCart();
              },
            ),
          ),
        ),
        Positioned(
          right:15,
          top:-5,
          child: IconButton(
            icon: Icon(Icons.close,color: Colors.grey,size:65.sp),
            onPressed: (){
              Navigator.of(context).pop();
            },
          ),
        ),
      ],
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
